<?php

require_once ROOT_DIR.'models/Todo.php';
require_once ROOT_DIR.'views/TodoJSON.php';
require_once ROOT_DIR.'controls/RightHelper.php';

/**
 * renders and echos the view for the todo with the given id.
 * @param int $id
 */
function getTodo($id, $linkedLists = false){
	
	if(is_null($linkedLists)) $linkedLists = false;
	
	$todo = Todo::getTodo($id);
	if(is_null($todo)){
		$res = new ResultJSON(true, session_id(), ERROR_TODO_DOES_NOT_EXIST);
		echo $res->renderView();
	} else {
		if($_SESSION['USER_NAME'] == 'admin' || $todo->getCreatorID() == $_SESSION['USER_ID'] || RightHelper::checkIfLinkedToTodo($todo)){
			echo TodoJSON::getView($todo, null, $linkedLists);
		} else {
			$res = new ResultJSON(true, session_id(), ERROR_NOT_ALLOWED);
			echo $res->renderView();
		}
	}
}

?>